#ifndef _TAB_SETTINGS_H_
#define _TAB_SETTINGS_H_

#include "llamaServerManager.h"
#include <QCheckBox>
#include <QLineEdit>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QComboBox>
class SettingsTab : public QWidget
{
    Q_OBJECT
public:
    explicit SettingsTab(QWidget *parent = nullptr);
    void loadSettings(const QString &settingsPath);
    void saveSettings(const QString &settingsPath);
    void setDefaultValues();

    // getter方法
    QString getApiKey() const;
    QString getUrl() const;
    QString getModel() const;

signals:
    void logMessage(const QString &message);

private slots:
    void onLocalAIToggled(bool checked);
    void selectModelFile();
    void testApiConnection();
    void onHistorySelected(int index);
    void saveToHistory();

private:
    void createUI();
    void updateComboBox();
    void saveHistoryToSettings();
    void useLatestHistory();

    QLineEdit *m_apiKeyEdit;
    QLineEdit *m_urlEdit;
    QLineEdit *m_modelEdit;
    QPushButton *m_testButton;
    QLineEdit *m_testResult;

    QCheckBox *m_useLocalAICheck;
    QLineEdit *m_modelPathEdit;
    QLineEdit *m_hostEdit;
    QLineEdit *m_portEdit;
    QLineEdit *m_tempEdit;
    QLineEdit *m_contextSizeEdit;
    QLineEdit *m_nglEdit;
    QPushButton *m_selectModelButton;
    QPushButton *m_resetButton;

    LlamaServerManager *m_llamaServer;
    QTimer *m_checkServerTimer;

    QPlainTextEdit *m_logOutput; // 日志输出指针

    QComboBox* m_historyCombo;  // 历史记录下拉框
    static const int MAX_HISTORY = 10;  // 最多保存10条历史记录
    
    // 存储历史记录的结构体
    struct OpenAISettings {
        QString apiKey;
        QString url;
        QString model;
    };
    QList<OpenAISettings> m_historySettings;  // 历史记录列表

public:
    void setLog(QPlainTextEdit *logOutput)
    {
        m_logOutput = logOutput;
    }
};

#endif // _TAB_SETTINGS_H_